<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>深拷贝-JSON序列化</title>
</head>

<body>
  <script>
    // 深拷贝实现方式一：JSON序列化（常用的方式）
    const obj = {
      name: '佩奇',
      family: {
        father: '猪爸爸'
      },
      hobby: ['跳泥坑', '唱歌']
    }
    

    //先转化为字符串JSON.stringify()
    console.log(JSON.stringify(obj))


    
    //再将字符串转化为数组JSON.parse
    const newobj=JSON.parse(JSON.stringify(obj))
    newobj.family.father='dad'
    console.log(newobj)


    // 注意事项：JSON.stringify 序列化的时候会忽略 function 、undefined
    const obj2 = {
      name: '佩奇',
      love: undefined,
      family: {
        father: '猪爸爸'
      },
      hobby: ['跳泥坑', '唱歌'],
      sayHi() {
        console.log('我会唱歌')
      }
    }

    const newobj2=JSON.parse(JSON.stringify(obj2))
    console.log(newobj2)

  </script>
</body>

</html>